#!/bin/sh

# $Id$

# Copyright 2013 Alan M. Watson.

if test $# != 0
then
  echo 1>&2 "error: usage: jatte import"
  exit 1
fi

start=$(date +%s)
echo 1>&2 "jatte import: starting."
echo 1>&2 "jatte import: importing from \"RAW/\" to \"FITS/\" and \"JPEG/\"."
echo 1>&2 "jatte import: using $JATTE_THREADS threads."

rm -rf FITS
mkdir FITS

rm -rf JPEG
mkdir JPEG

find RAW -type f ! -name ".*" -print0 | 
xargs -0 -L 1 -P $JATTE_THREADS sh -c '
  raw="$1"
  echo 1>&2 "jatte import: importing \"$raw\"."
  basename=$(echo "$raw" | sed "s:.*/::;s:\.[^.]*\$::")
  "$JATTE_LIBEXECDIR"/dcraw -c -4 -D -j "$raw" | 
  "$JATTE_LIBEXECDIR"/import-pass2 FITS/"$basename" | 
  convert -normalize -negate -quality 75 - JPEG/"$basename".jpeg
' --

echo 1>&2 "jatte import: creating \"exif.txt\"."
exiftool -common -T RAW >exif.txt
echo 1>&2 "jatte import: creating \"exif-full.txt\"."
exiftool -T RAW >exif-full.txt

end=$(date +%s)
n=$(echo $(find JPEG -type f ! -name ".*" -print0 | xargs -0 -L 1 sh -c "echo" | wc -l))
echo 1>&2 "jatte import: imported $n images in $(expr $end - $start) seconds."

echo 1>&2 "jatte import: done."

exit 0
